package models;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import models.Content.ContentType;
import siena.Filter;
import siena.Generator;
import siena.Id;
import siena.Model;
import siena.Query;
import siena.embed.At;
import siena.embed.Embedded;
import siena.embed.EmbeddedList;
import siena.embed.EmbeddedMap;

import com.google.appengine.api.datastore.Link;

import siena.core.Aggregated;
import siena.core.Many;
import siena.core.Owned;
import siena.core.lifecycle.PostInsert;
import siena.core.lifecycle.PostFetch;
import siena.core.lifecycle.PreInsert;

public class Message extends BaseModel {
		
	public User from;
	
	@Embedded
	public List<Receiver> receivers;
	
	public String text;
	
	@Embedded
	public List<SharedContent> contents;
	
	@EmbeddedList
	public class SharedContent {
		@At(0) public ContentType type;		
		@At(1) public Long id;
		@At(2) public String source;
	}
	
	@EmbeddedMap
	public class Receiver {
		@At(0) public Long id;
		@At(1) public String userName;
		@At(2) public String firstName;
		@At(3) public String lastName;
		
		public Receiver(User receiver) {
			this.id = receiver.id;
			this.userName = receiver.username;
			this.firstName = receiver.basic_information.firstName;
			this.lastName = receiver.basic_information.lastName;
		}
	}
	
	public void addReceiver(User receiver) {
		if(this.receivers == null) {
			this.receivers = new ArrayList<Receiver>();
		}
		
		this.receivers.add(new Receiver(receiver));
	}
	
	public List<Receiver> getReceivers() {
		return this.receivers;
	}
	
	public String clearText() {
		String clear = this.text != null ? this.text.trim() : "";
		clear = clear.replaceAll("\\r\\n|\\r|\\n", " ");
		
		return clear;
	}
}